﻿Imports System.Data.SqlClient
Imports DataSet_apaTableAdapters
Imports DataSet_apa
Partial Class Configura_Seguridad
    Inherits System.Web.UI.Page
    Dim pantalla As New pantallasTableAdapter
    Dim Usuarios As New aspnet_UsersTableAdapter
    Dim Usuario As New AccesosTableAdapter
    Dim sSQL As String
    Dim sdsAcceso As New SqlDataSource
    Protected Sub Configura_Seguridad_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ' Try
        'iIUsuario = Request.QueryString("Usuario")
        'Catch ex As Exception
        'iIUsuario = 0
        'End Try
        'If Not (iIUsuario = 0) Then
        'End If
    End Sub

    Protected Sub DropDLPantallas_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDLPantallas.Load
        If Not IsPostBack Then
            Dim REG As New pantallasDataTable
            REG = pantalla.GetPantallas()
            DropDLPantallas.DataSource = REG
            DropDLPantallas.DataTextField = "pantalla"
            DropDLPantallas.DataBind()
        End If
    End Sub
    Protected Sub DropDLUsuario_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDLUsuario.Load
        If Not IsPostBack Then
            Dim REG_Usuarios As New aspnet_UsersDataTable
            REG_Usuarios = Usuarios.GetAspnet_User()
            DropDLUsuario.DataSource = REG_Usuarios
            DropDLUsuario.DataTextField = "UserName"
            DropDLUsuario.DataBind()
        End If
    End Sub
    Protected Sub BtnActualiza_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnActualiza.Click
        Dim PantallaSecc As String = DropDLPantallas.SelectedValue
        Dim UsuarioSecc As String = DropDLUsuario.SelectedValue
        Dim CKAlta As String = CheckBAlta.Checked
        Dim CKActualiza As String = CheckBActualiza.Checked
        Dim CKElimina As String = CheckBElimina.Checked
        sSQL = "INSERT INTO Accesos(UserName, Pantalla, altas, actualiza, elimina) VALUES  ('" + UsuarioSecc + "'" & "," & "' " + PantallaSecc + "'   " & "," & "'" + CKAlta + " '   " & "," & "'" + CKActualiza + " '   " & "," & "'" + CKElimina + " ')"
        Try
            sdsAcceso.ConnectionString = ConfigurationManager.ConnectionStrings("GanaderiaConnectionString").ConnectionString
            sdsAcceso.InsertCommand = sSQL
            sdsAcceso.Insert()
        Catch ex As Exception

        End Try
        llenaGrid()
    End Sub
    Protected Sub DropDLUsuario_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDLUsuario.SelectedIndexChanged
        Dim REg_Usuario As New AccesosTableAdapter
        GVAccesos.DataSource = REg_Usuario.GetAccesosByUsuario(DropDLUsuario.SelectedValue)
        GVAccesos.DataBind()
    End Sub
    Sub llenaGrid()
        Dim REg_Usuario As New AccesosTableAdapter
        GVAccesos.DataSource = REg_Usuario.GetAccesosByUsuario(DropDLUsuario.SelectedValue)
        GVAccesos.DataBind()
    End Sub

    Protected Sub GVAccesos_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GVAccesos.RowDeleting
        Dim UsuarioSecc As String = DropDLUsuario.SelectedValue
        Dim cvePantalla As String = GVAccesos.Rows(e.RowIndex).Cells(1).Text
        sSQL = "DELETE FROM Accesos where UserName = " & "'" + UsuarioSecc + "'" & " and Pantalla = " & "'" + cvePantalla + "'"
        Try

            sdsAcceso.ConnectionString = ConfigurationManager.ConnectionStrings("GanaderiaConnectionString").ConnectionString
            sdsAcceso.DeleteCommand = sSQL
            sdsAcceso.Delete()

        Catch ex As Exception

        End Try
        llenaGrid()

    End Sub


    
End Class
